package com.hmdp.controller;


import com.hmdp.dto.Result;
import com.hmdp.entity.Blog;
import com.hmdp.entity.BlogComments;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogCommentsService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/comment")
public class BlogCommentsController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IBlogCommentsService blogCommentsService;
    @PostMapping()
    public Result save(@RequestBody BlogComments comment) {
//        if (comment.getId()==null){
//            Long id = UserHolder.getUser().getId();
//            comment.setUserId(id);
//            User user = userService.getById(id);
//            comment.setNickName(user.getNickName());
//            comment.setIcon(user.getIcon());
//
//            if (comment.getAnswerId()!=null) {    //判断回复则进行处理
//                Long aid = comment.getAnswerId();
//                BlogComments pComment = commentService.getById(aid);
//                if (pComment.getParentId() != null && pComment.getParentId()!=0L) {//祖宗id,如果当前回复的父级有祖宗，那么就设置相同的祖宗
//                    comment.setParentId(pComment.getParentId());
//                }
//            }else{
//                comment.setParentId(0L);
//            }
//        }
//        Long blogId = comment.getBlogId();
//        Blog blog = blogService.query().eq("id", blogId).one();
//        blog.setIsRead(false);
//        boolean saveOrUpdate = blogService.saveOrUpdate(blog);
//        boolean update = commentService.saveOrUpdate(comment);
//        if (update && saveOrUpdate) {
//            boolean id = blogService.update().setSql("comments=comments+1")
//                    .eq("id", comment.getBlogId()).update();
//            if (!id) {
//                return Result.fail("出现错误");
//            }
//        }
//
        return Result.ok();
    }
    @GetMapping("/tree/{id}")
    public Result queryTreeById(@PathVariable("id") Long id) {
        return blogCommentsService.queryTreeById(id);
    }

}
